﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algobox.JuicyLib.Apps.Stoxx.Files
{
    public class DividendForecast
    {
        public static Uri URL { get { return new Uri("http://www.stoxx.com/download/data/dividend_forecast.txt"); } }

        const string DEFAULT_DIV_TYPE = "Dividend";

        const int DATA_START_LINE = 1;
        const int DATA_MINIMUM_WIDTH = 13;

        const int DATA_Date = 0;
        const int DATA_Isin = 1;
        const int DATA_Sedol = 2;
        const int DATA_RIC = 3;
        const int DATA_InternalKey = 4;
        const int DATA_Name = 5;
        const int DATA_Country = 6;
        const int DATA_Currency = 7;
        const int DATA_DivType = 8;
        const int DATA_NetAmount = 9;
        const int DATA_GrossAmount = 10;
        const int DATA_DivCurrency = 11;
        const int DATA_Description = 12;



        /// <summary>
        /// Updates a listing dictionary with the compostion data from a specified file
        /// </summary>
        /// <param name="file">The opening TMI composition file</param>
        /// <param name="data">Dictionary of listing data to be updated</param>
        /// <param name="dataDate">date of the file being parsed</param>
        /// <returns></returns>
        static public List<Structure.StoxxDividend> ParseFile(ref string file)
        {
            List<Structure.StoxxDividend> data = new List<Structure.StoxxDividend>();

            // uses more memory... but easier to code!
            string[] lines = file.Split('\n');

            // get first line
            string[] line = lines[0].Split(';');

            decimal amount;
            DateTime date;
            Structure.StoxxDividend div;
            Algobox.JuicyLib.MarketData.MarketDataConstants.Currencies currency;

            // skip header lines and start extracting data from 3rd line (index 2)
            for (int i = DATA_START_LINE; i < lines.Length; ++i)
            {
                line = lines[i].Split(';');

                if (line.Length < DATA_MINIMUM_WIDTH)
                    continue;

                if (line[DATA_DivType] != DEFAULT_DIV_TYPE)
                    continue;

                // new div
                div = new Algobox.JuicyLib.Apps.Stoxx.Structure.StoxxDividend();

                // parse date string
                if (!DateTime.TryParseExact(line[DATA_Date], "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date))
                    continue;
                div.Date = date;

                if (!Decimal.TryParse(line[DATA_NetAmount], out amount))
                    continue;
                div.Amount = amount;

                if (!JuicyLib.MarketData.MarketDataConstants.TryParseCurrency(line[DATA_Currency].Trim(), out currency))
                    continue;
                div.Currency = currency;

                if (!JuicyLib.MarketData.MarketDataConstants.TryParseCurrency(line[DATA_DivCurrency].Trim(), out currency))
                    continue;
                div.DivCurrency = currency;

                div.ISIN = line[DATA_Isin];
                div.Sedol = line[DATA_Sedol];
                div.RIC = line[DATA_RIC];

                data.Add(div);

            }

            return data;
        }

    }
}
